package com.hex.ds.hdtp.starter;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@EnableAsync
@EnableCaching
@SpringBootApplication(scanBasePackages = {"com.hex.bigdata.hbda", "com.hex.hdtp.scf"})
@EnableScheduling
@RestController
public class HdtpApplication {

    @Value("${application.version}")
    private String appVersion;

    public static void main(String[] args) {
        new SpringApplicationBuilder(HdtpApplication .class)
                .beanNameGenerator(new ProGuardBeanNameGenerator()).run();
    }

    /**
     * 代码混淆后，包名、类名会存在重复，重写buildDefaultBeanName方法，获取全限定的类名
     */
    static class ProGuardBeanNameGenerator extends AnnotationBeanNameGenerator {
        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            return definition.getBeanClassName();
        }
    }

    @ApiOperation("获取平台信息")
    @GetMapping(value = "/info")
    public ResponseEntity<Object> getPlatformInfo() {
        Map<String, Object> platformInfo = new HashMap<>(2);
        platformInfo.put("name", "Hexinfo HDTP");
        platformInfo.put("version", appVersion);
        return ResponseEntity.ok(platformInfo);
    }
}
